package com.isesol.ime.service.mq.consumer;

import com.alibaba.rocketmq.client.consumer.listener.*;
import com.alibaba.rocketmq.client.exception.*;
import com.alibaba.rocketmq.common.message.*;
import com.alibaba.rocketmq.common.protocol.heartbeat.*;
import com.isesol.arch.common.mq.*;
import com.isesol.ime.service.*;
import com.isesol.ime.service.mq.topic.*;
import org.apache.commons.lang.*;
import org.springframework.stereotype.*;

import javax.annotation.*;
import java.util.*;

/**
 * 机床反馈
 *
 * @author peter zhang
 */
@Component
public class FeedbackConsumer extends BaseConsumer{

	@Resource
	FeedBackService feedBackService;

	@Override
	public Class<? extends Topic> getTopic(){

		return FeedBackSuccessTopic.class;

	}

	@Override
	public void doLogErrorConsumeMessage(final MsgObj msgObj){

		logger.info("FeedbackConsumer doLogErrorConsumeMessage");
 	}

	@Override
	public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context){

		try{

			if (msgs != null && !msgs.isEmpty()){

				Iterator msg = msgs.iterator();

				while (msg.hasNext()){

					MessageExt msgObj = (MessageExt) msg.next();
					String     body   = (String) SerializationUtils.deserialize(msgObj.getBody());

					feedBackService.consume(body);
				}
			}
		} catch (Exception e){

			logger.error("FeedbackConsumer error", e);
		}

		return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
	}

	public ConsumeConcurrentlyStatus doConsumeMessage(final MsgObj msgObj){

		return null;

	}

	@Override
	protected MessageModel getMessageModel(){

		return MessageModel.CLUSTERING;
	}

	@PostConstruct
	public void init() throws MQClientException{

		super.init();

		logger.info("【MQ Feedback init 】,messageModel={}", getMessageModel());

	}

	@PreDestroy
	public void destroy(){

		super.destroy();
	}
}
